package com.LachesismStorm.lachesism.commonTools;

import com.LachesismStorm.lachesism.core.util.AsyncUtil;
import com.LachesismStorm.lachesism.core.util.LachesismTools;
import lombok.SneakyThrows;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import static com.LachesismStorm.lachesism.core.common.enums.CommonEnum.DATE_FORMATTER;

/**
 * @Author lachesism
 * @Date 2024-12-26 17:54
 **/
public class getLunarHTML {
    @SneakyThrows
    public static void main(String[] args) {
        // 获取当前日期
        SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMATTER);
        AsyncUtil.executeAsync(() -> {
            Calendar calendar = Calendar.getInstance();
            for (int i = 0; i < 2500; i++) {
                // 在当前日期基础上加一天
                // 格式化日期
                String time = formatter.format(calendar.getTime()); // 获取加一天后的日期时间字符串
                String url = "https://365zyg.com/lhl/" + time + ".html";
                String htmlContent = null;
                try {
                    htmlContent = LachesismTools.sendGet(url, null, null);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                // 文件路径
                String filePath = "/Users/yuno/java/webApp/html/" + time + ".html";
                try {
                    writeToFile(htmlContent, filePath);
                    System.out.println(time + ".html 文件已成功生成！");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                calendar.add(Calendar.DATE, 1);
            }
        });
        AsyncUtil.executeAsync(() -> {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, 2499);
            for (int i = 0; i < 2500; i++) {
                // 在当前日期基础上加一天
                // 格式化日期
                String time = formatter.format(calendar.getTime()); // 获取加一天后的日期时间字符串
                String url = "https://365zyg.com/lhl/" + time + ".html";
                String htmlContent = null;
                try {
                    htmlContent = LachesismTools.sendGet(url, null, null);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                // 文件路径
                String filePath = "/Users/yuno/java/webApp/html/" + time + ".html";
                try {
                    writeToFile(htmlContent, filePath);
                    System.out.println(time + ".html 文件已成功生成！");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                calendar.add(Calendar.DATE, 1);
            }
        });
    }

    public static void writeToFile(String content, String filePath) throws IOException {
        // 创建文件对象
        File file = new File(filePath);

        // 创建 BufferedWriter，指定文件路径
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
            // 写入内容到文件
            writer.write(content);
        }
    }


}
